home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_toolbx.arc / INSERT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-30  |  788 b   |  28 lines

  1. /*  insert.c - insertion sort function */
  2. /*  uses pointers to actual data elements and a compare function */
  3. #include "stdio.h"
  4.  
  5. int insert(pa,na,pcomp)
  6.   char    *pa[] ;     /* number of pointers to elements to be sorted */
  7.   int    na    ;         /* number of elements to be sorted */
  8.   int    (*pcomp) ()  ;        /* pointer to compare function */
  9.   {
  10.      int   i  ;         /* indeces for loops */
  11.      int   j  ;         /* for inner loop - optomize */
  12.      char  *ptemp ;        /* temporary storage for one pointer */
  13.  
  14.      for( i=1 ; i < na ; i = i + 1 )
  15.     {  /* insert the i-th element into the storage array */
  16.        ptemp = pa[i] ;
  17.        j = j - 1 ;
  18.        while( (j >= 0 ) && ( (*pcomp) (ptemp,pa[j]) <= 0 ) )
  19.           {  pa[j+1] = pa[j] ;
  20.          j = j - 1 ;
  21.           }
  22.        pa[j+1] = ptemp ;
  23.     }
  24.   }
  25.  
  26.  
  27.  
  28.